import { Camera, ResolutionPolicy, screen, UITransform, view } from "cc";
import { InjectableComponent } from "../../inject";

export default class UIAdapterComp extends InjectableComponent {
    public transform!: UITransform;

    public camera!: Camera;

    public portrait!: boolean;

    onLoad() {
        super.onLoad();

        this.transform = this.getComponent(UITransform);
        this.camera = this.getComponentInChildren(Camera);

        this.resize();
    }

    resize() {
        let dr = view.getDesignResolutionSize();
        var s = screen.windowSize;

        var rw = s.width;
        var rh = s.height;
        var finalW = rw;
        var finalH = rh;

        if ((rw / rh) > (dr.width / dr.height)) {
            finalH = dr.height;
            finalW = finalH * rw / rh;
            this.portrait = false;
        } else {
            finalW = dr.width;
            finalH = finalW * rh / rw;
            this.portrait = true;
        }

        view.setDesignResolutionSize(finalW, finalH, ResolutionPolicy.UNKNOWN);

        this.transform!.width = finalW;
        this.transform!.height = finalH;
    }
}